﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.CustomerView.Common;
using LinkCard.Utils;

namespace LinkCard.CustomerView
{
    public partial class PartnerDetail : System.Web.UI.Page
    {
        public AppEnv _Instance = AppEnv.Instance();
        public string pnrImage = "";
        public string pnrIntroduction = "";
        public string pnrName = "";
        public string pnrWebsite = "";
        public string pnrProductCount = "0";
        public string pnrGmap = "";
        public string[] pnrListAddress = new string[0];

        private Int64 _supplierID = -1;

        private int _ownerDealCurrentPage = 1; 
        private int _ownerDealPageSize = 4;
        private int _ownerDealTotalPage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Page.RouteData.Values["sid"] != null)
                _supplierID = ConvertUtility.ToInt64(Page.RouteData.Values["sid"]);
            if (Request.Params["page"] != null)
                _ownerDealCurrentPage = ConvertUtility.ToInt32(Request.Params["page"]);

            // Load supplier informations
            LoadSupplierInfo(_supplierID);

            // Load list deal of supplier (box bottom)
            LoadListPagedOwnerDeals(_supplierID, _ownerDealCurrentPage, _ownerDealPageSize, ref _ownerDealTotalPage);

            Page.Title = pnrName+" - Đối tác - LinkCard";
            Page.MetaDescription = pnrName+" - " + pnrIntroduction + " - Đối tác - LinkCard";
        }

        private void LoadSupplierInfo(Int64 supplierID)
        {
            try
            {
                if (supplierID < 0) return;

                Dao.SupplierDao db = new Dao.SupplierDao();
                var _supplier = db.GetById(supplierID);
                if (_supplier != null)
                {
                    var ownerDeals = new Dao.DealDao().GetAllBySupplier(_supplier.SupplierID);

                    pnrListAddress = _supplier.Address.Split('|');
                    pnrIntroduction = HttpUtility.HtmlDecode(_supplier.Introduction);
                    pnrName = HttpUtility.HtmlDecode(_supplier.Name);
                    pnrProductCount = ownerDeals.Count.ToString();
                    pnrWebsite = HttpUtility.HtmlDecode(_supplier.Website);
                    //if (_supplier.Logo == "")
                    //{
                    //    pnrImage = "";
                    //    img_banner.Visible = false;
                    //}
                    //else
                    //{
                    //    pnrImage = _Instance["SupplierUploadPath"] + _supplier.Logo;
                    //    img_banner.Visible = true;
                    //    img_banner.Src = pnrImage;
                    //}
                    pnrGmap = _supplier.Gmap;
                }
            }
            catch (Exception ex)
            {
                // out message here
            }
        }

        private void LoadListPagedOwnerDeals(Int64 supplierID, int currentPage, int pageSize, ref int totalPage)
        {
            //try
            //{
            //    if (supplierID <= 0) return;

            //    Dao.DealDao db = new Dao.DealDao();
            //    var allDeals = db.GetAllBySupplierPaged(supplierID, currentPage,pageSize, ref totalPage);
            //    rptOwnerDeals.DataSource = allDeals;
            //    rptOwnerDeals.DataBind();

            //    // Show Pager control
            //    List<KeyValuePair<string, object>> lstParam = new List<KeyValuePair<string, object>>();
            //    lstParam.Add(new KeyValuePair<string, object>("mid", "2"));
            //    lstParam.Add(new KeyValuePair<string, object>("sid", supplierID));

            //    ltrPaging.Text = Common.Pager.Create(currentPage, totalPage, lstParam);
            //}
            //catch (Exception ex)
            //{
            //    // Out message here
            //}
        }

        public string GetFirstImageFromAlbum(object album)
        {
            string reval = "";
            try
            {
                var imgList = Utils.Utilities.GetProductImage(album.ToString());
                if (imgList != null && imgList.Count > 0)
                    reval = imgList[0];
            }
            catch (Exception ex)
            {
                return "";
            }
            return reval;

        }

        public string GetSubString(object str, int length)
        {
            if (str == null)
                return "";

            else if (str.ToString().Length <= length)
                return str.ToString();
            else
                return (str.ToString().Substring(0, length) + "...");

        }
    }
}